# -*- coding: utf-8 -*-
#coding=utf-8


def singleton(className):
    def wrapped():
        it =  className.__dict__.get('__it__')
        if it is not None:
            return it

        className.__it__=className()
        return className.__it__
    return wrapped

def inplace_change(filename, old_string, new_string):
    s=open(filename).read()
    if old_string in s:
        s=s.replace(old_string, new_string)
        f=open(filename, 'w')
        f.write(s)
        f.flush()
        f.close()

def uuidgen():
    import uuid
    return str(uuid.uuid4())

def hashgen(s):
    import hashlib
    return hashlib.sha224(s).hexdigest()


def runcmd(cmd):
    import subprocess
    proc = subprocess.Popen(cmd,
                     shell=True,
                     stdout=subprocess.PIPE,
                     stderr=subprocess.STDOUT)
    stdout = proc.stdout.read()
    retval = proc.wait()
    return (retval, stdout)

